package cn.signalpha.modules.base.controller.api;

import cn.signalpha.common.cache.ConfigCache;
import cn.signalpha.common.cache.DictCache;
import cn.signalpha.common.cache.ParamCache;
import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.jackson.JsonUtil;
import cn.signalpha.core.tool.utils.ObjectUtil;
import cn.signalpha.core.tool.utils.StringPool;
import cn.signalpha.core.tool.utils.StringUtil;
import cn.signalpha.modules.auth.dto.UserInfo;
import cn.signalpha.modules.auth.utils.TokenUtil;
import cn.signalpha.modules.pay.service.PayService;
import cn.signalpha.modules.resource.builder.sms.SmsBuilder;
import cn.signalpha.modules.shop.entity.Seller;
import cn.signalpha.modules.shop.entity.SellerAddress;
import cn.signalpha.modules.shop.service.CityDeliveryService;
import cn.signalpha.modules.shop.service.OrderService;
import cn.signalpha.modules.shop.service.SellerAddressService;
import cn.signalpha.modules.shop.service.SellerService;
import cn.signalpha.modules.shop.vo.SellerDeliveryConfigVO;
import cn.signalpha.modules.system.entity.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_OPEN_NAME)
@Api(value = "开放接口", tags = "开放接口")
public class ApiOpenController extends BaseController {

	private final PayService payService;
	private final SmsBuilder smsBuilder;
	private final SellerService sellerService;
	private final OrderService orderService;
	private final SellerAddressService sellerAddressService;
	private final CityDeliveryService cityDeliveryService;

	@GetMapping("/param")
	@ApiOperation(value = "获取参数", notes = "传入参数key")
	public R params(@RequestParam String key) {
		return R.data(ParamCache.getValue(key));
	}

	@GetMapping("/dict/list")
	@ApiOperation(value = "获取字典", notes = "传入字典code")
	public R<List<Dict>> dictBizList(@RequestParam String code) {
		return R.data(DictCache.getList(code));
	}

	@GetMapping("/config")
	@ApiOperation(value = "获取配置", notes = "传入配置key")
	public R config(@RequestParam String key) {
		return R.data(ConfigCache.getValue(key));
	}
	@GetMapping("/preview/token")
	@ApiOperation(value = "获取预览token", notes = "")
	public R previewToken() {
		UserInfo userInfo = new UserInfo();
		userInfo.setId(0L);
		userInfo.setRoleId("preview");
		return R.data(TokenUtil.createAuthInfo(userInfo));
	}

	@GetMapping("/create/shop")
	public R createShop() {
		//判断是新增还是修改
		Seller seller = sellerService.getById("1864928088647919999");
		SellerDeliveryConfigVO sellerDeliveryConfig = JsonUtil.parse(seller.getDeliveryConfig(), SellerDeliveryConfigVO.class);
		SellerAddress sellerAddress = sellerAddressService.getById(sellerDeliveryConfig.getAddressId());
		Map<String, Object> resultMap;
		if(StringUtil.isBlank(sellerDeliveryConfig.getShopId())){
			resultMap = cityDeliveryService.createSeller(seller.getId().toString(),
				seller.getName() + "-" + sellerAddress.getName(),
				seller.getMobile(),
				sellerAddress.getMobile(),
				sellerAddress.getAddress(),
				sellerAddress.getLng(),
				sellerAddress.getLat()
			);
		}else {
			resultMap = cityDeliveryService.updateSeller(sellerDeliveryConfig.getShopId(),
				seller.getName() + "-" + sellerAddress.getName(),
				sellerAddress.getMobile(),
				sellerAddress.getAddress(),
				sellerAddress.getLng(),
				sellerAddress.getLat()
			);
		}
		sellerDeliveryConfig.setShopId(ObjectUtil.isNotEmpty(resultMap.get("shop_id")) ? String.valueOf(resultMap.get("shop_id")) : StringPool.EMPTY);
		sellerDeliveryConfig.setOpenId(ObjectUtil.isNotEmpty(resultMap.get("open_id")) ? String.valueOf(resultMap.get("open_id")) : StringPool.EMPTY);
		seller.setDeliveryConfig(JsonUtil.toJson(sellerDeliveryConfig));
		sellerService.updateById(seller);
		sellerService.removeCacheById(seller.getId());
		return R.status(true);
	}
	@GetMapping("/push/order")
	public R pushOrder(@RequestParam String orderNo) {
		return R.data(cityDeliveryService.pushOrder(orderService.getById(orderNo)));
	}

}
